﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using NUnit.Framework;

namespace Savage.DotNetLibrary.TestDotNetLibrary.ProtocolLibraryTests
{
    [TestFixture]
    public class TestHttpWrapper
    {
        [Test]
        public void TestGet()
        {
            Uri uri = new Uri("http://www.google.co.uk");

            ProtocolLibrary.HttpWrapper httpWrapper = new ProtocolLibrary.HttpWrapper(uri);
            
            //Check the the uri is the same in the webRequest object.
            Assert.AreEqual(uri, httpWrapper.WebRequest.RequestUri);

            string response = httpWrapper.Get();
            Console.Out.WriteLine("TestHttpWrapper.TestGet: response=" + response);
            //The response should contain the I'm Feeling Lucky button that is displayed on the page.
            Assert.IsTrue(response.IndexOf("<input name=btnI type=submit value=\"I'm Feeling Lucky\">", StringComparison.Ordinal) >= 0);

        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestGetNullArgumentException()
        {
            Uri uri = null;
            ProtocolLibrary.HttpWrapper httpWrapper = new ProtocolLibrary.HttpWrapper(uri);
        }

        [Test]
        public void TestPost()
        {
            Uri uri = new Uri("http://www.bbc.co.uk/cgi-perl/weather/search/new_search.pl");
            ProtocolLibrary.HttpWrapper httpWrapper = new ProtocolLibrary.HttpWrapper(uri);
            //Check the the uri is the same in the webRequest object.
            Assert.AreEqual(uri, httpWrapper.WebRequest.RequestUri);
            
            string search_query = "exeter";
            string match = "Set Exeter as my Weather homepage";
            string postData = "search_query=" + search_query;

            string response = httpWrapper.Post(postData);
            Console.Out.WriteLine("TestHttpWrapper.TestPost: response=" + response);
            
            Assert.IsTrue(response.IndexOf(match, StringComparison.Ordinal) > -1, "Could not find the phrase '" + search_query + "' in the returned web page.");
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestSendWebRequestNullArgumentException()
        {
            ProtocolLibrary.HttpWrapper.SendWebRequest(null);
        }
    }
}
